package com.yunwaikeji.gy_tool_library.utils;

import android.text.Editable;
import android.text.TextWatcher;

import com.yunwaikeji.gy_tool_library.info.GYEditTextInfo;
import com.yunwaikeji.gy_tool_library.view.GYTextView;

import java.util.Arrays;
import java.util.List;

public class GYEditTextUtils implements GYEditTextInfo {
    public static GYEditTextUtils gyEditTextUtils;
    public static GYEditTextUtils getInstance(){
        if (gyEditTextUtils==null){
            gyEditTextUtils = new GYEditTextUtils();
        }
        return gyEditTextUtils;
    }

    public GYEditTextUtils setTextLengthChangedListener(TextLengthChangedListener textLengthChangedListener, GYTextView... gyTextViews) {
        return setTextLengthChangedListener(textLengthChangedListener, Arrays.asList(gyTextViews));
    }

    public GYEditTextUtils setTextLengthChangedListener(TextLengthChangedListener textLengthChangedListener, List<? extends GYTextView> gyTextViewList) {
        if (textLengthChangedListener != null) {
            for (GYTextView gyTextView : gyTextViewList) {
                gyTextView.addTextChangedListener(new TextWatcher() {
                    @Override
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                    }

                    @Override
                    public void onTextChanged(CharSequence s, int start, int before, int count) {

                    }

                    @Override
                    public void afterTextChanged(Editable s) {
                        boolean isLength = true;
                        for (GYTextView gyTextView : gyTextViewList) {
                            if (!gyTextView.getViewSet().fitLength()) {
                                isLength = false;
                                break;
                            }
                        }
                        textLengthChangedListener.textLengthChange(isLength);
                    }
                });
            }
        }
        return this;
    }

    public interface TextLengthChangedListener {
        void textLengthChange(boolean isLength);
    }
}
